home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GEN32.PAK / MISC.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  97 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   misc.c
  9. //
  10. //  PURPOSE:  Contains all helper functions "global" to the application.
  11. //
  12. //  FUNCTIONS:
  13. //    CenterWindow - Center one window over another.
  14. //
  15. //  COMMENTS:
  16. //
  17. //
  18.  
  19. #include <windows.h>            // required for all Windows applications
  20. #include "globals.h"            // prototypes specific to this application
  21.  
  22.  
  23.  
  24. //
  25. //  FUNCTION: CenterWindow(HWND, HWND)
  26. //
  27. //  PURPOSE:  Center one window over another.
  28. //
  29. //  PARAMETERS:
  30. //    hwndChild - The handle of the window to be centered.
  31. //    hwndParent- The handle of the window to center on.
  32. //
  33. //  RETURN VALUE:
  34. //
  35. //    TRUE  - Success
  36. //    FALSE - Failure
  37. //
  38. //  COMMENTS:
  39. //
  40. //    Dialog boxes take on the screen position that they were designed
  41. //    at, which is not always appropriate. Centering the dialog over a
  42. //    particular window usually results in a better position.
  43. //
  44.  
  45. BOOL CenterWindow(HWND hwndChild, HWND hwndParent)
  46. {
  47.     RECT    rcChild, rcParent;
  48.     int     cxChild, cyChild, cxParent, cyParent;
  49.     int     cxScreen, cyScreen, xNew, yNew;
  50.     HDC     hdc;
  51.  
  52.     // Get the Height and Width of the child window
  53.     GetWindowRect(hwndChild, &rcChild);
  54.     cxChild = rcChild.right - rcChild.left;
  55.     cyChild = rcChild.bottom - rcChild.top;
  56.  
  57.     // Get the Height and Width of the parent window
  58.     GetWindowRect(hwndParent, &rcParent);
  59.     cxParent = rcParent.right - rcParent.left;
  60.     cyParent = rcParent.bottom - rcParent.top;
  61.  
  62.     // Get the display limits
  63.     hdc = GetDC(hwndChild);
  64.     cxScreen = GetDeviceCaps(hdc, HORZRES);
  65.     cyScreen = GetDeviceCaps(hdc, VERTRES);
  66.     ReleaseDC(hwndChild, hdc);
  67.  
  68.     // Calculate new X position, then adjust for screen
  69.     xNew = rcParent.left + ((cxParent - cxChild) / 2);
  70.     if (xNew < 0)
  71.     {
  72.         xNew = 0;
  73.     }
  74.     else if ((xNew + cxChild) > cxScreen)
  75.     {
  76.         xNew = cxScreen - cxChild;
  77.     }
  78.  
  79.     // Calculate new Y position, then adjust for screen
  80.     yNew = rcParent.top  + ((cyParent - cyChild) / 2);
  81.     if (yNew < 0)
  82.     {
  83.         yNew = 0;
  84.     }
  85.     else if ((yNew + cyChild) > cyScreen)
  86.     {
  87.         yNew = cyScreen - cyChild;
  88.     }
  89.  
  90.     // Set it, and return
  91.     return SetWindowPos(hwndChild,
  92.                         NULL,
  93.                         xNew, yNew,
  94.                         0, 0,
  95.                         SWP_NOSIZE | SWP_NOZORDER);
  96. }
  97.